package controller;

import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import pojo.Article;
import pojo.User;
import service.ArticleService;
import service.UserService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserService userService;

    public ArticleService getArticleService() {
        return articleService;
    }

    public void setArticleService(ArticleService articleService) {
        this.articleService = articleService;
    }

    @RequestMapping(value = "/query.do")
    public String query(Article article, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("title" + article.getTitle());
        System.out.println("进入");
        List<Article> list = articleService.getALlArticle(article);
        List<User> userList = userService.getAllUser((User) request.getSession().getAttribute("User"));
        for (Article article1 : list) {
            System.out.println(article1);
        }
        request.setAttribute("artciles", list);
        request.setAttribute("user",userList.get(0));
        return "shouye";
    }

    @RequestMapping(value = "/add.do")
    public ModelAndView add(HttpSession session,Article article, HttpServletRequest request, Model model, @RequestParam(required = false, value = "img_1") MultipartFile img1,
                            @RequestParam(required = false, value = "img_2") MultipartFile img2, @RequestParam(required = false, value = "img_3") MultipartFile img3) {
        ModelAndView mav = new ModelAndView();
        String path = request.getSession().getServletContext().getRealPath("images" + File.separator + "uploadfiles");
        if (img1 != null) {
            if (img1.getSize() > 1 * 1024 * 1024) {
                request.setAttribute("uploadrrormsg", "文件不能超过1M");
                mav.setViewName("xiewenzhang");
            }
            String oldFileName = img1.getOriginalFilename();
            String extension = FilenameUtils.getExtension(oldFileName);
            System.out.println(extension);
            List<String> prefixs = new ArrayList<>();
            prefixs.add("jpg");
            prefixs.add("png");
            prefixs.add("jpeg");
            prefixs.add("pneg");
            prefixs.add("JPG");
            if (prefixs.contains(prefixs) == false) {
                request.setAttribute("uploadrrormsg", " * 文件类型只能是图片");
                mav.setViewName("xiewenzhang");
            }
            //封装文件对象
            String filenaem = article.getImg1() + "_img1." + extension;
            File file = new File(path, filenaem);
            try {
                img1.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
                request.setAttribute("uploadrrormsg", e.getMessage());
            }
            article.setImg1(filenaem);
            mav.setViewName("xiewenzhang");
        }

        if (img2 != null) {
            if (img2.getSize() > 1 * 1024 * 1024) {
                request.setAttribute("uploadrrormsg", "文件不能超过1M");
                mav.setViewName("xiewenzhang");
            }
            String oldFileName = img2.getOriginalFilename();
            String extension = FilenameUtils.getExtension(oldFileName);
            List<String> prefixs = new ArrayList<>();
            prefixs.add("jpg");
            prefixs.add("png");
            prefixs.add("jpeg");
            prefixs.add("pneg");
            prefixs.add("JPG");
            if (prefixs.contains(prefixs) == false) {
                request.setAttribute("uploadrrormsg", " * 文件类型只能是图片");
                mav.setViewName("xiewenzhang");
            }
            //封装文件对象
            String filenaem = article.getImg2()+ "_img2." + extension;
            File file = new File(path, filenaem);
            try {
                img2.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
                request.setAttribute("uploadrrormsg", e.getMessage());
            }
            article.setImg2(filenaem);
            mav.setViewName("xiewenzhang");
        }

        if (img3 != null) {
            if (img3.getSize() > 1 * 1024 * 1024) {
                request.setAttribute("uploadrrormsg", "文件不能超过1M");
                mav.setViewName("xiewenzhang");
            }
            String oldFileName = img3.getOriginalFilename();
            String extension = FilenameUtils.getExtension(oldFileName);
            List<String> prefixs = new ArrayList<>();
            prefixs.add("jpg");
            prefixs.add("png");
            prefixs.add("jpeg");
            prefixs.add("pneg");
            prefixs.add("JPG");
            if (prefixs.contains(prefixs) == false) {
                request.setAttribute("uploadrrormsg", " * 文件类型只能是图片");
                mav.setViewName("xiewenzhang");
            }
            //封装文件对象
            String filenaem = article.getImg3() + "_img3." + extension;
            File file = new File(path, filenaem);
            try {
                img3.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
                request.setAttribute("uploadrrormsg", e.getMessage());
            }
            article.setImg3(filenaem);
            mav.setViewName("xiewenzhang");
        }
        //好像是有点问题
            Article article1=new Article();
        article.setReleasetime(new Date());
          article.setBeclick(0);
          article.setBereport(0);
          article.setViews(0);

         User user= (User) session.getAttribute("User");
         user.getId();
         article.setUserid(user.getId());

        int i = articleService.addArticle(article);

        if (i > 0) {
            System.out.println("增加成功");
            model.addAttribute("addarticle", "发布成功");

        } else {

            System.out.println("增加失败");
            model.addAttribute("addarticle", "发布失败");
        }
        return mav;
    }

    @RequestMapping("/delete.do")
    public String delete(int id, Model model) {
        int i = articleService.deleteArticle(id);
        if (i > 0) {
            System.out.println("删除成功");
            model.addAttribute("deletearticle", "删除成功");

        } else {
            System.out.println("删除失败");
            model.addAttribute("deletearticle", "删除成功");
        }
        return "";
    }

    @RequestMapping("/update.do")
    public String update(Article article, Model model) {
        int i = articleService.updateArticle(article);
        if (i > 0) {
            System.out.println("修改成功");
            model.addAttribute("updatearticle", "修改成功");

        } else {
            System.out.println("修改失败");
            model.addAttribute("updatearticle", "修改成功");
        }

        return "redirect:/article/query.do";
    }
    @RequestMapping("/queryArtofUser.do")
    public String queryArtAndUser(HttpSession session){
        List<Article> list = articleService.getALlArticleAndArticleofUser();
        session.setAttribute("artlist",list);
        return "shouye";
    }


    /**
     * ajax  到时候改成 responsebody  用户界面显示所有此用户发表的文章
     * @param request
     * @param userid
     * @return
     */
    @RequestMapping("/whocreateArticle.do")
    @ResponseBody
    public List<Article> whoCreate(HttpServletRequest request,@RequestParam(defaultValue = "0") Integer userid){
        List<Article> artList = articleService.selectUserofArticle(userid);
        return artList;
    }



}
